﻿using System;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;

using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Pages.Model;

namespace Migration.Classes
{
	public static class MigrationUtils
	{
		public static string GetUrlName(string input, string language = "")
		{
            var result = Regex.Replace(input.Trim(), @"[^\w_-]", "-").ToLower();
			return result;
		}

		public static bool IsGuid(string expression)
		{
			bool isGuid = false;
			if (!string.IsNullOrEmpty(expression))
			{
				Regex guidRegEx = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
				isGuid = guidRegEx.IsMatch(expression);
			}
			return isGuid;
		}

		public static string GetFullPagePath(PageNode pageNode)
		{
			string result = pageNode.UrlName;
			PageNode parent = pageNode.Parent;
			while (parent != null && parent.Id != SiteInitializer.FrontendRootNodeId)
			{
				result = parent.UrlName + "/" + result;
				parent = parent.Parent;
			}

			return "~/" + result;
		}

		public static string GetLocalizableSkipMessage(string name, string language)
		{
			if (!String.IsNullOrEmpty(language))
				return String.Format("Skipping \"<b>{0}</b>\" - language <b>{1}</b>, already exists.", name, language.ToUpperInvariant());
			else
				return String.Format("Skipping \"<b>{0}</b>\", already exists.", name);
		}

		public static string GetLocalizableImportMessage(string name, string language)
		{
			if (!String.IsNullOrEmpty(language))
				return String.Format("Imported \"<b>{0}</b>\" - language <b>{1}</b>.", name, language.ToUpperInvariant());
			else
				return String.Format("Imported \"<b>{0}</b>\".", name);
		}

		public static string GetSafeTextFileContents(string filePath)
		{
			string result = String.Empty;

			try
			{
				result = File.ReadAllText(filePath);
			}
			catch (IOException) { }
			catch (System.Security.SecurityException) { }
			catch (UnauthorizedAccessException) { };

			return result;
		}

		public static DateTime GetSafeDate()
		{
			return DateTime.MinValue;
		}

		public static string GetRandomId()
		{
			return "_" + Guid.NewGuid().ToString().Replace("-", String.Empty);
		}
	}

	public class ParsedRegisterTag
	{
		public string Src { get; set; }
		public string TagPrefix { get; set; }
		public string TagName { get; set; }

		public void Retrieve(string registerTag)
		{
			this.Src = this.TagPrefix = this.TagName = String.Empty;

			var srcAttributeMatch = Regex.Match(registerTag, @"Src=""?([\w\._\/~-]*)""?", RegexOptions.IgnoreCase | RegexOptions.Singleline);
			if (srcAttributeMatch.Success && srcAttributeMatch.Groups.Count > 1)
				this.Src = srcAttributeMatch.Groups[1].Value;

			var tagPrefixAttributeMatch = Regex.Match(registerTag, @"TagPrefix=""?([\w\._\/~-]*)""?", RegexOptions.IgnoreCase | RegexOptions.Singleline);
			if (tagPrefixAttributeMatch.Success && tagPrefixAttributeMatch.Groups.Count > 1)
				this.TagPrefix = tagPrefixAttributeMatch.Groups[1].Value;

			var tagNameAttributeMatch = Regex.Match(registerTag, @"TagName=""?([\w\._\/~-]*)""?", RegexOptions.IgnoreCase | RegexOptions.Singleline);
			if (tagNameAttributeMatch.Success && tagNameAttributeMatch.Groups.Count > 1)
				this.TagName = tagNameAttributeMatch.Groups[1].Value;
		}
	}
}
